package com.achievo.know.web.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.achievo.know.application.SystemConfigApplication;
import com.achievo.know.domain.SystemConfig;
import com.dayatang.domain.InstanceFactory;

public class SystemFilter implements Filter {
	
	private static SystemConfigApplication systemConfigApplication;

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest r=(HttpServletRequest)request;
		HttpSession session=r.getSession();
		String title=(String)session.getAttribute("title");
		if(title==null || "".equals(title)){
			SystemConfig config=systemConfigApplication.getConfig();
			session.setAttribute("title", config.getTitle());
			session.setAttribute("headTitle", config.getHeadTitle());
		}
		
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		if (systemConfigApplication == null) {
			systemConfigApplication = InstanceFactory.getInstance(SystemConfigApplication.class);
		}
	}

}
